package orest.back.membermanage;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import net.ssta.common.DataBaseConfig;
import net.ssta.common.LoginUser;
import net.ssta.core.base.BaseException;
import net.ssta.core.base.ErrorCode;
import net.ssta.core.base.ServletBase;
import net.ssta.core.db.PagingInfo;
import net.ssta.core.db.Record;
import net.ssta.core.db.RecordSet;
import net.ssta.core.web.RequestKit;

import com.yztc.rc.api.framework.entity.User;
import com.yztc.rc.api.framework.entity.UserAccount;
import com.yztc.rc.api.framework.manager.UserManager;

/**
 * 用户积分管理
 * @author Administrator
 *
 */
public class RegisterUserManager  extends ServletBase {
 
	
	private static final long serialVersionUID = 1L;
	 

	public void defaultAction(HttpServletRequest request,HttpServletResponse response) 
	{
		userList(request,response);
	}
 
	public void userList(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			RequestKit kit=new RequestKit(request);
			Record searchData=kit.getRecordWithPrefix("search_");
			PagingInfo pageingInfo = kit.getPagingInfo(10,"^(?!.*(result)|(LTPAToken)).*$");
			RegisterUserHandler handler=new RegisterUserHandler();
			RecordSet rs=handler.registerList(searchData,pageingInfo);
			request.setAttribute("RECORDSET", rs);
			request.setAttribute("searchData", searchData);
			request.setAttribute("pagingInfo", pageingInfo);
	        this.forward("/back/registerUser/userInfoList.jsp");
	     }catch(Exception se){
		     throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
	
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws BaseException
	 */
	public void addUser(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			RequestKit kit=new RequestKit(request);
			Record userData=kit.getRecordWithPrefix("user_");
			RegisterUserHandler handler=new RegisterUserHandler();
			handler.addUser(userData);
			String path=request.getContextPath();
			response.sendRedirect(path+"/back/registerUser/addUser.jsp?flag=true");
	     }catch(Exception se){
		     throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
	 
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws BaseException
	 */
	public void ajaxSendMail(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		PrintWriter    out =null;
		try{ 
			 response.setCharacterEncoding("UTF-8");
			 out = response.getWriter();
			RequestKit kit=new RequestKit(request);
			String userId=kit.getParameter("userId", "");
			
			RegisterUserHandler handler=new RegisterUserHandler();
			Record r=RegisterUserHandler.getUserById(userId);
			String serverAddress=request.getServerName();
			if(!"80".equals(request.getServerPort())){
				serverAddress+=":"+request.getServerPort();
			}
			handler.sendMail(r.getString("EMAIL"), r.getString("VERIFYID"), "register", serverAddress, r.getString("LOGINNAME",""));
			out.print("true");
			out.flush();
			out.close();
		 }catch(UnsupportedEncodingException se){
			 System.out.println(se);
			 out.print("true");
				out.flush();
				out.close();
	     }catch(MessagingException se){
	    	 System.out.println(se);
	    	 out.print("false");
				out.flush();
				out.close();
	     }catch(Exception se){
	    	 System.out.println(se);
	    	 out.print("false");
				out.flush();
				out.close();
	     }
	}
	
	
	
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws BaseException
	 */
	public void modUser(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			RequestKit kit=new RequestKit(request);
			Record userData=kit.getRecordWithPrefix("user_");
			RegisterUserHandler handler=new RegisterUserHandler();
			handler.modUser(userData);
			String path=request.getContextPath();
			response.sendRedirect(path+"/back/registerUser/modUser.jsp?flag=true&userId="+userData.getString("USERID"));
			
	     }catch(Exception se){
		     throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws BaseException
	 */
	public void applyUpgrade(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			RequestKit kit=new RequestKit(request);
			Record userData=kit.getRecordWithPrefix("user_");
			RegisterUserHandler handler=new RegisterUserHandler();
			handler.applyUpgrade(userData);
			String path=request.getContextPath();
			response.sendRedirect(path+"/membercenter/upgrade/selectUpgrade.jsp");
	     }catch(Exception se){
		     throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
	
	
	
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws BaseException
	 */
	public void applyList(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			String path=request.getContextPath();
			RequestKit kit=new RequestKit(request);
			Record searchData=kit.getRecordWithPrefix("search_");
			PagingInfo pageingInfo = kit.getPagingInfo(10,"^(?!.*(result)|(LTPAToken)).*$");
			RegisterUserHandler handler=new RegisterUserHandler();
			RecordSet rs=handler.applyList(searchData,pageingInfo);
			request.setAttribute("RECORDSET", rs);
			request.setAttribute("searchData", searchData);
			request.setAttribute("pagingInfo", pageingInfo);
	        this.forward("/back/registerUser/applyList.jsp");
	     }catch(Exception se){
		     throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws BaseException
	 */
	public void unitList(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			String path=request.getContextPath();
			RequestKit kit=new RequestKit(request);
			Record searchData=kit.getRecordWithPrefix("search_");
			PagingInfo pageingInfo = kit.getPagingInfo(10,"^(?!.*(result)|(LTPAToken)).*$");
			UnitSearchHandler handler=new UnitSearchHandler();
			RecordSet rs=handler.unitList(searchData,pageingInfo);
			request.setAttribute("RECORDSET", rs);
			request.setAttribute("searchData", searchData);
			request.setAttribute("pagingInfo", pageingInfo);
	        this.forward("/back/unit/unitList.jsp");
	     }catch(Exception se){
		     throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws BaseException
	 */
	public void expertList(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			String path=request.getContextPath();
			RequestKit kit=new RequestKit(request);
			Record searchData=kit.getRecordWithPrefix("search_");
			PagingInfo pageingInfo = kit.getPagingInfo(10,"^(?!.*(result)|(LTPAToken)).*$");
			UnitSearchHandler handler=new UnitSearchHandler();
			RecordSet rs=handler.expertList(searchData,pageingInfo);
			request.setAttribute("RECORDSET", rs);
			request.setAttribute("searchData", searchData);
			request.setAttribute("pagingInfo", pageingInfo);
	        this.forward("/back/unit/expertList.jsp");
	     }catch(Exception se){
		     throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
	
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws BaseException
	 */
	public void setUnitUser(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		String flag="false";
		PrintWriter    out =null;
		try{ 
			 RequestKit kit=new RequestKit(request);
			 response.setCharacterEncoding("UTF-8");
			 out = response.getWriter();
				
		
			String userId=kit.getParameter("userId", "");
			String unitId=kit.getParameter("unitId", "");
			
			RegisterUserHandler handler=new RegisterUserHandler();
			handler.setUnitUser(userId,unitId);
			flag="true";
		   out.print(flag);
		   out.flush();
		   out.close();
	     }catch(Exception se){
	    	 out.print(flag);
			   out.flush();
			   out.close();
	     }
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws BaseException
	 */
	public void delUser(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		String flag="false";
		PrintWriter    out =null;
		try{ 
			 RequestKit kit=new RequestKit(request);
			 response.setCharacterEncoding("UTF-8");
			 out = response.getWriter();
			 String userId=kit.getParameter("userId", "");
			
			RegisterUserHandler handler=new RegisterUserHandler();
			handler.updateUserDel(userId,true);
			flag="true";
		   out.print(flag);
		   out.flush();
		   out.close();
	     }catch(Exception se){
	    	 out.print(flag);
			   out.flush();
			   out.close();
	     }
	}
	
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws BaseException
	 */
	public void openUser(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		String flag="false";
		PrintWriter    out =null;
		try{ 
			 RequestKit kit=new RequestKit(request);
			 response.setCharacterEncoding("UTF-8");
			 out = response.getWriter();
			 String userId=kit.getParameter("userId", "");
			
			RegisterUserHandler handler=new RegisterUserHandler();
			handler.updateUserDel(userId,false);
			flag="true";
		   out.print(flag);
		   out.flush();
		   out.close();
	     }catch(Exception se){
	    	 out.print(flag);
			   out.flush();
			   out.close();
	     }
	}
	

}
